package com.wd.live.ui.dailog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bw.base.tool.selectpic.GlideEngine;
import com.bw.base.tool.selectpic.ImageFileCropEngine;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.config.SelectModeConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.wd.live.R;

import java.util.ArrayList;
import java.util.List;


/**
 *
 */
public class UserHeaderDialog extends Dialog {

    View.OnClickListener mCancelListener;

    private Context context;
    private onItemClickListener listeners;

    private View viewCancel;
    private TextView tvCamera;
    private TextView tvAlbum;
    private PictureSelector pictureSelector;
    private Activity activity;
    private List<String> mPhoto;
    OnResultCallbackListener<LocalMedia> onResultCallbackListener = new OnResultCallbackListener<LocalMedia>() {
        @Override
        public void onResult(ArrayList<LocalMedia> result) {
            send(result);
        }

        @Override
        public void onCancel() {
        }
    };

    public UserHeaderDialog(Context context, Activity activity) {
        this(context, 0);
        this.context = context;
        this.activity = activity;
    }


    private UserHeaderDialog(Context context, int themeResId) {
        super(context, R.style.dialog_style);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_photo);
        initDialogWindow(getWindow(), Gravity.BOTTOM, 1.0f);
        setCanceledOnTouchOutside(true);
        initView();
    }

    public void setOnCancelListener(@Nullable View.OnClickListener listener) {
        mCancelListener = listener;
    }


    private void initView() {

        mPhoto = new ArrayList<>();
        pictureSelector = PictureSelector.create(activity);

        viewCancel = findViewById(R.id.view_cancel);
        tvCamera = findViewById(R.id.tv_camera);
        tvAlbum = findViewById(R.id.tv_album);


        viewCancel.setOnClickListener(view -> {
            if (mCancelListener != null) {
                mCancelListener.onClick(view);
            }
            dismiss();
        });

        tvCamera.setOnClickListener(view -> {
            takePic();
            dismiss();
        });

        tvAlbum.setOnClickListener(view -> {
            choosePic();
            dismiss();
        });

    }

    public void setNumber(int number) {
        mPhoto.clear();
    }


    private void choosePic() {
        pictureSelector
                .openGallery(SelectMimeType.ofImage())
                .setImageEngine(GlideEngine.createGlideEngine())
                .setSelectionMode(SelectModeConfig.SINGLE)
                .setCropEngine(new ImageFileCropEngine())
                .forResult(onResultCallbackListener);
    }

    private void takePic() {
        pictureSelector
                .openCamera(SelectMimeType.ofImage())
                .setCropEngine(new ImageFileCropEngine())
                .forResult(onResultCallbackListener);
    }

    private void send(ArrayList<LocalMedia> result) {
        ArrayList<String> list = new ArrayList<>();
        String imgPath = result.get(0).getCutPath();
        list.add(imgPath);
        if (listeners != null) listeners.onFile(list);
    }


    public void initDialogWindow(Window window, int gravity, float ratio) {
        if (window != null) {
            window.setGravity(gravity);
//            window.setWindowAnimations(R.style.BottomDialogStyle);
            window.getDecorView().setPadding(0, 0, 0, 0);
            //获得window窗口的属性
            WindowManager.LayoutParams lp = window.getAttributes();
            //设置窗口宽度为充满全屏
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            //设置窗口高度为包裹内容
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            //将设置好的属性set回去
            window.setAttributes(lp);
        }
    }


    public void setOnSelectListener(onItemClickListener listener) {
        this.listeners = listener;
    }

    public interface onItemClickListener {
        default void onFile(List<String> url) {
        }

    }

}
